package tencent

import (
	"ddns/config"
	"fmt"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
)

var myConfig *config.Configuration
var myClient *dnspod.Client

func init() {
	myConfig = config.GET()
	credential := common.NewCredential(
		myConfig.SecretId,
		myConfig.SecretKey,
	)
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "dnspod.tencentcloudapi.com"
	client, _ := dnspod.NewClient(credential, "", cpf)
	myClient = client

}

func DescribeRecord() *dnspod.DescribeRecordResponse {
	request := dnspod.NewDescribeRecordRequest()
	request.RecordId = common.Uint64Ptr(myConfig.RecordId)
	request.Domain = common.StringPtr(myConfig.Domain)
	response, err := myClient.DescribeRecord(request)
	if err != nil {
		fmt.Printf("解析记录查询失败! %s", err)
		return nil
	}
	return response
}

func ModifyRecord(ip string) *dnspod.ModifyRecordResponse {
	request := dnspod.NewModifyRecordRequest()
	request.RecordId = common.Uint64Ptr(myConfig.RecordId)
	request.Domain = common.StringPtr(myConfig.Domain)
	request.SubDomain = common.StringPtr(myConfig.SubDomain)
	request.RecordType = common.StringPtr("A")
	request.RecordLine = common.StringPtr("默认")
	request.Value = common.StringPtr(ip)
	response, err := myClient.ModifyRecord(request)
	if err != nil {
		fmt.Printf("解析记录修改失败! %s", err)
		return nil
	}
	return response
}
